<template>
  <!-- home首页 -->
  <div class="home-wrap">
    <div class="home-header" v-show="showHeader">
      <span>首页</span>
    </div>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in bannerList">
        <img :src="item.picUrl" alt="" />
      </van-swipe-item>
    </van-swipe>

    <yx-title @click="gotoList('/bargainList')">
      全民砍价 <van-icon name="arrow"
    /></yx-title>

    <div class="home-artical">
      <ul>
        <li v-for="item in articalList" :key="item.id">
          <img :src="item.pic" alt="" />
          <h5>{{ item.title }}</h5>
          <p>{{ item.descript }}</p>
        </li>
      </ul>
    </div>

    <yx-title @click="gotoList('/recommendList')">
      人气推荐 <van-icon name="arrow"
    /></yx-title>

    <div class="recommend-wrap">
      <yx-gooditem
        v-for="item in recommendList"
        :key="item.id"
        :item="item"
      ></yx-gooditem>
    </div>
  </div>
</template>
<script>
import { getBanner, getArticalList, getRecommendList } from "@/utils/http/home";
import { ref, reactive, toRefs } from "vue";

import yxTitle from "@/components/yxTitle.vue";
import yxGooditem from "@/components/yxGooditem.vue";
import { useRouter } from "vue-router";
export default {
  setup(props) {
    const router = useRouter();
    //定义路由
    const showHeader = ref(false);
    //吸顶效果的显示隐藏
    const data = reactive({
      bannerList: [],
      //轮播图数据
      articalList: [],
      //文章列表数据
      recommendList: [],
    });
    getBanner().then((res) => {
      // console.log(res);
      data.bannerList = res.data;
    });
    //获取轮播列表
    getArticalList().then((res) => {
      console.log(res);
      data.articalList = res.data;
    });
    //获取文章列表

    getRecommendList().then((res) => {
      console.log(res);
      data.recommendList = res.data.slice(9, 13);
    });
    //获取人气推荐列表

    const gotoList = (val) => {
      router.push(val);
    };
    //跳转列表页

    window.onscroll = () => {
      let top = document.documentElement.scrollTop;
      // console.log(document.documentElement.scrollTop);
      console.log(top);
      if (top >= 200) {
        showHeader.value = true;
      } else {
        showHeader.value = false;
      }
    };
    //整个页面滚动的时候 执行这个事件 打印页面卷进去的距离

    return { ...toRefs(data), gotoList, showHeader };
  },
  components: {
    yxTitle,
    yxGooditem,
  },
};
</script>
<style lang="scss">
.home-artical {
  width: 100%;
  overflow: scroll;
  ul {
    display: flex;

    li {
      // flex:1;
      // flex是
      // flex-grow 默认是不放大 0
      // flex-shrink  默认是1 缩小
      // flex-basis 按照自身的宽度的多少倍去放大
      // 这三个的合写

      width: 320px;
      flex-shrink: 0;
      //让每个li都不缩小 是自己的本身的宽度
      margin-left: 10px;
      border-radius: 3px;
      overflow: hidden;
      img {
        width: 100%;
        height: auto;
      }
    }
  }
}
.recommend-wrap {
  display: flex;
  justify-content: space-evenly;
  flex-wrap: wrap;
  > div {
    width: 48%;
  }
}
.home-header {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: greenyellow;
  z-index: 999;
}
.my-swipe {
  img {
    width: 100%;
    height: 300px;
  }
}
.home-wrap {
  padding-bottom: 80px;
}
</style>
